local BaseScene = class("BaseScene",function()
    return display.newScene("MainScene");
end);

function BaseScene:ctor()
    self.listeners = {};
    mgrs.log:printFuncCall();
end

function BaseScene:onEnter()
    mgrs.log:printFuncCall();
    -- updatescene 已经添加了
    --cc.FileUtils:getInstance():addSearchPath("pkg/");
	--cc.FileUtils:getInstance():addSearchPath("res/");
    --cc.FileUtils:getInstance():addSearchPath("code/");
end

function BaseScene:onExit()
    mgrs.log:printFuncCall();
    for k,v in ipairs(self.listeners) do
        mgrs.event:removeEventListener(v);
    end
    self.listeners = {};
end

function BaseScene:onEnterTransitionFinish()
    mgrs.log:printFuncCall();
    self:loadMainNode();
    self:setPurgeCachedDataEvent();
    self:setAppErrorEvent();
    self:setTipsLayerTouchEvent();
    self:setAndroidKeyEvent();
end

function BaseScene:loadMainNode()
    local mainNode = display.newNode();
    mainNode:setContentSize(display.size);
    local tmpTable = {};
    for k,v in pairs(enums.Layer) do
        tmpTable[v] = k;
    end
    for i,v in ipairs(tmpTable) do
        local panel = display.newNode();
        panel:setContentSize(display.size);
        panel:setAnchorPoint(cc.p(0.5,0.5));
        panel:setPosition(mgrs.wnd.visibleRect.center);
        panel:setTouchEnabled(false);
        mainNode:addChild(panel);
    end
    mgrs.wnd:addMianNode(mainNode);
    self:addChild(mainNode);
end

function BaseScene:setPurgeCachedDataEvent()
	local handle = mgrs.event:addEventListener(enums.Event.PurgeCacheData,handler(self,self.purgeCachedEventCbk));
	table.insert(self.listeners,handle);
end

function BaseScene:setAppErrorEvent()
    local handle = mgrs.event:addEventListener(enums.Event.APPError,handler(self,self.showExitAlert));
	table.insert(self.listeners,handle);
end

function BaseScene:purgeCachedEventCbk()
    cc.Director:getInstance():purgeCachedData();
end

function BaseScene:setTipsLayerTouchEvent()
    local tipLayer = mgrs.wnd:getLayer(enums.Layer.Tips);
    tipLayer:setTouchEnabled(true);
    tipLayer:setTouchSwallowEnabled(false);
    tipLayer:addNodeEventListener(cc.NODE_TOUCH_EVENT,function(event)
        if event.name == "began" then
            mgrs.event:dispatchEvent({name = enums.Event.TipsTouchBegan});
            return true;
        elseif event.name == "ended" then
            mgrs.event:dispatchEvent({name = enums.Event.TipsTouchEnded});
        end
    end);
end

function BaseScene:setAndroidKeyEvent()
	if device.platform == "android"  or device.platform == "windows" then
        local node = display.newNode();
        node:setKeypadEnabled(true)
        node:addNodeEventListener(cc.KEYPAD_EVENT, function(event)
            if event.key == "back" then
                self:showExitAlert({title = "Confirm Exit",info = "Are you sure exit game ?"});
            elseif event.key == "menu" then
                mgrs.log:printDebug("menu");
            end
        end)
        self:addChild(node);
    end
end

function BaseScene:showExitAlert(event)
    self:upLoadErrLog(event.info);
    device.showAlert(event.title,event.info,{"YES", "NO"},function(event)
        if event.buttonIndex == 1 then
            cc.Director:getInstance():endToLua();
            if device.platform == "windows" or device.platform == "mac" then
                os.exit();
            end
        else
            device.cancelAlert();
        end
    end)
end

function BaseScene:upLoadErrLog(log)
    local fileName = os.date("%Y%m%d%H%M%S");
    fileName = "log/" .. fileName .. ".dat";
    mgrs.gameState:init(fileName);
    mgrs.gameState:addValue(fileName,"error",log);
    local traceback = debug.traceback("", 2);
    mgrs.gameState:addValue(fileName,"traceback",traceback);
    mgrs.gameState:saveByFileName(fileName);
    local fliePath = device.writablePath .. "a.jpg";
    mgrs.http:uploadFile("http://192.168.1.120/xampp/HttpGame/upload.php",filePath,"Image/jpeg");
end

return BaseScene;


--endregion
